package com.example.demo1.service;

import com.example.demo1.common.WrapMapper;
import com.example.demo1.common.Wrapper;
import com.example.demo1.entity.Teacher;
import com.example.demo1.mapper.TeacherMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TeacherService {
    @Autowired
    public TeacherMapper teacherMapper;

    //1、注册
    public Wrapper registerTeach(Teacher teacher) {
        // (1) 查询教师是否存在
        Teacher existingTeach = teacherMapper.selectTeachName(teacher.getTc_name());
        if (existingTeach != null){
            return WrapMapper.error("教师已存在！");
        }
        // (2) 插入教师数据
        int rows = teacherMapper.insertTeach(teacher);
        if (rows > 0){
            return WrapMapper.ok(teacher);
        }else {
            return WrapMapper.error("教师注册失败！");
        }
    }

    //2、登录
    public Wrapper loginTeach(String tc_name, String tc_id) {
        // (1) 根据用户名查询用户是否存在
        Teacher teacher = teacherMapper.getTeachName(tc_name);
        if (teacher == null) {
            return WrapMapper.error("教师不存在");
        }
        return WrapMapper.ok(teacher);
    }
}
